﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GBMS.MODEL;
using Telerik.Web.UI;
using System.Data;
using KPIS.GERP.GBMS.BLL;

using System.Web.Configuration;
//using MySql.Data.MySqlClient;
using KPIS.GERP.GBMS.APP_UI.Common;

namespace KPIS.GERP.GBMS.APP_UI.Form
{

    public partial class BGMKPIResultList : System.Web.UI.Page
    {
        private BGMKPIResultListBLL cmd = new BGMKPIResultListBLL();
        private ComboBLL comboBLL = new ComboBLL();
        Main main = new Main();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadYear();
                LoadJobSiteCombo();
                LoadTarget();
                LoadrblKPIListType();
            }
            
        }

        private void LoadgrdMonth()
        {
            int intStratType = Convert.ToInt32(rblKPIType.SelectedValue);
            int intTargetType = Convert.ToInt32(cboRadTarget.SelectedValue);

            switch (intStratType)
            {
                case 1:
                    if (cboRadOutput.SelectedIndex > 0)
                    {
                        int Output = Convert.ToInt32(cboRadOutput.SelectedValue);
                        grdRadMonth.DataSource = cmd.GridProject(intStratType, intTargetType, Output);
                        grdRadMonth.DataBind();
                    }
                    else
                    {
                        main.MessageAlert(Page, "กรุณาเลือกผลผลิต");
                    }
                    break;

                case 2:
                    if (cboRadMainAct.SelectedIndex > 0)
                    {
                        int MainAct = Convert.ToInt32(cboRadMainAct.SelectedValue);
                        grdRadMonth.DataSource = cmd.GridProject(intStratType, intTargetType, MainAct);
                        grdRadMonth.DataBind();
                    }
                    else
                    {
                        main.MessageAlert(Page, "กรุณาเลือกกิจกรรมหลัก");
                    }
                    break;

                case 3:
                    if (cboRadSubAct.SelectedIndex > 0)
                    {
                        int SubAct = Convert.ToInt32(cboRadSubAct.SelectedValue);
                        grdRadMonth.DataSource = cmd.GridProject(intStratType, intTargetType, SubAct);
                        grdRadMonth.DataBind();
                    }
                    else
                    {
                        main.MessageAlert(Page, "กรุณาเลือกกิจกรรมย่อย");
                    }
                    break;

                case 4:
                    if (cboRadBgmProjSeq.SelectedIndex > 0)
                    {
                        int ProjSeq = Convert.ToInt32(cboRadBgmProjSeq.SelectedValue);
                        grdRadMonth.DataSource = cmd.GridProject(intStratType, intTargetType, ProjSeq);
                        grdRadMonth.DataBind();
                    }
                    else
                    {
                        main.MessageAlert(Page, "กรุณาเลือกโครงการ");
                    }
                    break;

                case 5:
                    if (cboRadBgmProjSeq.SelectedIndex > 0)
                    {
                        int ProjSeqRequest = Convert.ToInt32(cboRadBgmProjSeq.SelectedValue);
                        grdRadMonth.DataSource = cmd.GridProject(intStratType, intTargetType, ProjSeqRequest);
                        grdRadMonth.DataBind();
                    }
                    else
                    {
                        main.MessageAlert(Page, "กรุณาเลือกโครงการ");
                    }
                    break;
            }
            
        }

        private void LoadgrdQuarter()
        {
            if (cboRadYear.SelectedIndex == 0)
            {
                main.MessageAlert(Page, "กรุณาเลือกปี");
            }
            else
            {
                int intStratType = Convert.ToInt32(rblKPIType.SelectedValue);
                int intTargetType = Convert.ToInt32(cboRadTarget.SelectedValue);

                switch (intStratType)
                {
                    case 1:
                        if (cboRadOutput.SelectedIndex > 0)
                        {
                            int Output = Convert.ToInt32(cboRadOutput.SelectedValue);
                            grdRadQuarter.DataSource = cmd.GridProject(intStratType, intTargetType, Output);
                            grdRadQuarter.DataBind();
                        }
                        else
                        {
                            main.MessageAlert(Page, "กรุณาเลือกผลผลิต");
                        }
                        break;

                    case 2:
                        if (cboRadMainAct.SelectedIndex > 0)
                        {
                            int MainAct = Convert.ToInt32(cboRadMainAct.SelectedValue);
                            grdRadQuarter.DataSource = cmd.GridProject(intStratType, intTargetType, MainAct);
                            grdRadQuarter.DataBind();
                        }
                        else
                        {
                            main.MessageAlert(Page, "กรุณาเลือกกิจกรรมหลัก");
                        }
                        break;

                    case 3:
                        if (cboRadSubAct.SelectedIndex > 0)
                        {
                            int SubAct = Convert.ToInt32(cboRadSubAct.SelectedValue);
                            grdRadQuarter.DataSource = cmd.GridProject(intStratType, intTargetType, SubAct);
                            grdRadQuarter.DataBind();
                        }
                        else
                        {
                            main.MessageAlert(Page, "กรุณาเลือกกิจกรรมย่อย");
                        }
                        break;

                    case 4:
                        if (cboRadBgmProjSeq.SelectedIndex > 0)
                        {
                            int ProjSeq = Convert.ToInt32(cboRadBgmProjSeq.SelectedValue);
                            grdRadQuarter.DataSource = cmd.GridProject(intStratType, intTargetType, ProjSeq);
                            grdRadQuarter.DataBind();
                        }
                        else
                        {
                            main.MessageAlert(Page, "กรุณาเลือกโครงการ");
                        }
                        break;

                    case 5:
                        if (cboRadBgmProjSeq.SelectedIndex > 0)
                        {
                            int ProjSeqRequest = Convert.ToInt32(cboRadBgmProjSeq.SelectedValue);
                            grdRadQuarter.DataSource = cmd.GridProject(intStratType, intTargetType, ProjSeqRequest);
                            grdRadQuarter.DataBind();
                        }
                        else
                        {
                            main.MessageAlert(Page, "กรุณาเลือกโครงการ");
                        }
                        break;
                }
            }
        }

        private void LoadgrdAnnual()
        {
            int intStratType = Convert.ToInt32(rblKPIType.SelectedValue);
            int intTargetType = Convert.ToInt32(cboRadTarget.SelectedValue);

            switch (intStratType)
            {
                case 1:
                    if (cboRadOutput.SelectedIndex > 0)
                    {
                        int Output = Convert.ToInt32(cboRadOutput.SelectedValue);
                        grdRadAnnual.DataSource = cmd.GridProject(intStratType, intTargetType, Output);
                        grdRadAnnual.DataBind();
                    }
                    else
                    {
                        main.MessageAlert(Page, "กรุณาเลือกผลผลิต");
                    }
                    break;

                case 2:
                    if (cboRadMainAct.SelectedIndex > 0)
                    {
                        int MainAct = Convert.ToInt32(cboRadMainAct.SelectedValue);
                        grdRadAnnual.DataSource = cmd.GridProject(intStratType, intTargetType, MainAct);
                        grdRadAnnual.DataBind();
                    }
                    else
                    {
                        main.MessageAlert(Page, "กรุณาเลือกกิจกรรมหลัก");
                    }
                    break;

                case 3:
                    if (cboRadSubAct.SelectedIndex > 0)
                    {
                        int SubAct = Convert.ToInt32(cboRadSubAct.SelectedValue);
                        grdRadAnnual.DataSource = cmd.GridProject(intStratType, intTargetType, SubAct);
                        grdRadAnnual.DataBind();
                    }
                    else
                    {
                        main.MessageAlert(Page, "กรุณาเลือกกิจกรรมย่อย");
                    }
                    break;

                case 4:
                    if (cboRadBgmProjSeq.SelectedIndex > 0)
                    {
                        int ProjSeq = Convert.ToInt32(cboRadBgmProjSeq.SelectedValue);
                        grdRadAnnual.DataSource = cmd.GridProject(intStratType, intTargetType, ProjSeq);
                        grdRadAnnual.DataBind();
                    }
                    else
                    {
                        main.MessageAlert(Page, "กรุณาเลือกโครงการ");
                    }
                    break;

                case 5:
                    if (cboRadBgmProjSeq.SelectedIndex > 0)
                    {
                        int ProjSeqRequest = Convert.ToInt32(cboRadBgmProjSeq.SelectedValue);
                        grdRadAnnual.DataSource = cmd.GridProject(intStratType, intTargetType, ProjSeqRequest);
                        grdRadAnnual.DataBind();
                    }
                    else
                    {
                        main.MessageAlert(Page, "กรุณาเลือกโครงการ");
                    }
                    break;
            }
            
        }

        private void LoadYear()
        {
            int CurrYear = SystemInfo._CurrentYearReq;
            cboRadYear.Items.Clear();
            for (int i = CurrYear + 5; i >= CurrYear - 5; i--)
            {
                cboRadYear.Items.Add(new RadComboBoxItem(i.ToString(), i.ToString()));
            }
            cboRadYear.Items.Insert(0, new RadComboBoxItem("----- เลือกปี -----", ""));
            
        }

        private void LoadJobSiteCombo()
        {
            DataTable dt = cmd.ListJobsiteKPI();
            cboRadJobSite.DataSource = dt;
            cboRadJobSite.DataBind();
            cboRadJobSite.Items.Insert(0, new RadComboBoxItem("----- เลือกหน่วยงาน -----", ""));
        }

        private void LoadBgmProjSeq(int intJobsite)
        {
            int intYear = Convert.ToInt32(cboRadYear.SelectedValue);
            int intProjectType = Convert.ToInt32(rblKPIType.SelectedValue);

            DataTable dt = cmd.ListBgmProjSeq(intYear, intJobsite, intProjectType); 
            cboRadBgmProjSeq.DataSource = dt;
            cboRadBgmProjSeq.DataBind();
            cboRadBgmProjSeq.Items.Insert(0, new RadComboBoxItem("----- เลือกโครงการ -----", ""));
        }

        public void LoadSubActCombo(int intYear)
        {
            DataTable dt = cmd.ListSubActKPI(intYear);
            cboRadSubAct.DataSource = dt;
            cboRadSubAct.DataBind();
            cboRadSubAct.Items.Insert(0, new RadComboBoxItem("----- เลือกกิจกรรมย่อย -----", ""));
        }

        private void LoadMainActCombo(int intYear)
        {
            DataTable dt = cmd.ListMainActKPI(intYear);
            cboRadMainAct.DataSource = dt;
            cboRadMainAct.DataBind();
            cboRadMainAct.Items.Insert(0, new RadComboBoxItem("----- เลือกกิจกรรมหลัก -----", ""));
        }

        private void LoadOutputCombo(int intYear)
        {
            DataTable dt = cmd.ListOutputKPI(intYear);
            cboRadOutput.DataSource = dt;
            cboRadOutput.DataBind();
            cboRadOutput.Items.Insert(0, new RadComboBoxItem("----- เลือกผลผลิต -----", ""));
        }

        private void LoadTarget()
        {
            DataTable dt = cmd.ListTargetKPI();
            cboRadTarget.DataSource = dt;
            cboRadTarget.DataBind();
            cboRadTarget.Items.Insert(0, new RadComboBoxItem("----- เลือกเป้าหมาย -----", ""));
        }

        private void LoadrblKPIListType()
        {

            DataTable dt = cmd.LoadKPIListType();
            rblKPIType.DataSource = dt; 
            rblKPIType.DataBind();
            rblKPIType.SelectedValue = "1";
            
        }

        protected void rblKPIType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (rblKPIType.SelectedValue)
            {
                case "1":
                    pnlListItem.GroupingText = "เลือกผลผลิต";
                    Label1.Text = "";
                    cboRadYear.ClearSelection();
                    cboRadTarget.ClearSelection();
                    cboRadJobSite.Visible = false;
                    cboRadSubAct.Visible = false;
                    cboRadMainAct.Visible = false;
                    cboRadOutput.Visible = false;//
                    lblProj.Visible = false;
                    cboRadBgmProjSeq.Visible = false;
                    lblTarget.Visible = false;
                    cboRadTarget.Visible = false;
                    grdRadMonth.Visible = false;
                    grdRadQuarter.Visible = false;
                    grdRadAnnual.Visible = false;
                    btnRadSubmit.Visible = false;
                    break;

                case "2":
                    pnlListItem.GroupingText = "เลือกกิจกรรมหลัก";
                    Label1.Text = "";
                    cboRadYear.ClearSelection();
                    cboRadTarget.ClearSelection();
                    cboRadJobSite.Visible = false;
                    cboRadSubAct.Visible = false;
                    cboRadMainAct.Visible = false;
                    cboRadOutput.Visible = false;
                    lblProj.Visible = false;
                    cboRadBgmProjSeq.Visible = false;
                    lblTarget.Visible = false;
                    cboRadTarget.Visible = false;
                    grdRadMonth.Visible = false;
                    grdRadQuarter.Visible = false;
                    grdRadAnnual.Visible = false;
                    btnRadSubmit.Visible = false;
                    break;

                case "3":
                    pnlListItem.GroupingText = "เลือกกิจกรรมย่อย";
                    Label1.Text = "";
                    cboRadYear.ClearSelection();
                    cboRadTarget.ClearSelection();
                    cboRadJobSite.Visible = false;
                    cboRadSubAct.Visible = false;
                    cboRadMainAct.Visible = false;
                    cboRadOutput.Visible = false;
                    lblProj.Visible = false;
                    cboRadBgmProjSeq.Visible = false;
                    lblTarget.Visible = false;
                    cboRadTarget.Visible = false;
                    grdRadMonth.Visible = false;
                    grdRadQuarter.Visible = false;
                    grdRadAnnual.Visible = false;
                    btnRadSubmit.Visible = false;
                    break;

                case "4":
                    pnlListItem.GroupingText = "เลือกโครงการ";
                    Label1.Text = "";
                    cboRadYear.ClearSelection();
                    cboRadJobSite.ClearSelection();
                    cboRadTarget.ClearSelection();
                    cboRadJobSite.Visible = false;
                    cboRadSubAct.Visible = false;
                    cboRadMainAct.Visible = false;
                    cboRadOutput.Visible = false;
                    lblProj.Visible = false;
                    cboRadBgmProjSeq.Visible = false;
                    lblTarget.Visible = false;
                    cboRadTarget.Visible = false;
                    grdRadMonth.Visible = false;
                    grdRadQuarter.Visible = false;
                    grdRadAnnual.Visible = false;
                    btnRadSubmit.Visible = false;
                    break;

                case "5":
                    pnlListItem.GroupingText = "เลือกโครงการ";
                    Label1.Text = "";
                    cboRadYear.ClearSelection();
                    cboRadJobSite.ClearSelection();
                    cboRadTarget.ClearSelection();
                    cboRadJobSite.Visible = false;
                    cboRadSubAct.Visible = false;
                    cboRadMainAct.Visible = false;
                    cboRadOutput.Visible = false;
                    lblProj.Visible = false;
                    cboRadBgmProjSeq.Visible = false;
                    lblTarget.Visible = false;
                    cboRadTarget.Visible = false;
                    grdRadMonth.Visible = false;
                    grdRadQuarter.Visible = false;
                    grdRadAnnual.Visible = false;
                    btnRadSubmit.Visible = false;
                    break;
                    
            }
        }

        protected void cboRadYear_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (cboRadYear.SelectedIndex == 0)
            {
                hideControl();
                hideGrid();
            }

            if (cboRadYear.SelectedIndex > 0)
            {
                int intYear = Convert.ToInt32(cboRadYear.SelectedValue);
                LoadSubActCombo(intYear);
                LoadMainActCombo(intYear);
                LoadOutputCombo(intYear);
            
            
                cboRadJobSite.SelectedIndex = 0;
                cboRadBgmProjSeq.SelectedIndex = 0;
                cboRadSubAct.SelectedIndex = 0;
                cboRadMainAct.SelectedIndex = 0;
                cboRadOutput.SelectedIndex = 0;
                cboRadTarget.SelectedIndex = 0;
                btnRadSubmit.Visible = false;
                Label1.Visible = true;
            
            GridColumnGroup columnGroup = grdRadAnnual.MasterTableView.ColumnGroups.FindGroupByName("annual");
            columnGroup.HeaderText = "ปี " + cboRadYear.SelectedValue;
            grdRadMonth.Visible = false;
            grdRadQuarter.Visible = false;
            grdRadAnnual.Visible = false;

            switch(rblKPIType.SelectedValue)
            {
                case "1":
                    Label1.Text = "ผลผลิต :";
                    cboRadOutput.Visible = true;
                    lblTarget.Visible = true;
                    cboRadTarget.Visible = true;
                    break;

                case "2":
                    Label1.Text = "กิจกรรมหลัก :";
                    cboRadMainAct.Visible = true;
                    lblTarget.Visible = true;
                    cboRadTarget.Visible = true;
                    break;

                case "3":
                    Label1.Text = "กิจกรรมย่อย :";
                    cboRadSubAct.Visible = true;
                    lblTarget.Visible = true;
                    cboRadTarget.Visible = true;
                    break;

                case "4":
                    Label1.Text = "หน่วยงาน :";
                    cboRadJobSite.Visible = true;
                    break;

                case "5":
                    Label1.Text = "หน่วยงาน :";
                    cboRadJobSite.Visible = true;
                    break;
                
            }
            }
        }

        protected void cboRadJobSite_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (cboRadJobSite.SelectedIndex > 0)
            {
                lblProj.Visible = true;
                cboRadBgmProjSeq.Visible = true;
                lblTarget.Visible = true;
                cboRadTarget.Visible = true;
                cboRadTarget.SelectedIndex = 0;

                int intJobsite = Convert.ToInt32(cboRadJobSite.SelectedValue);
                LoadBgmProjSeq(intJobsite);
                btnRadSubmit.Visible = false;
                grdRadMonth.Visible = false;
                grdRadQuarter.Visible = false;
                grdRadAnnual.Visible = false;
            }
            else
            {
                main.MessageAlert(Page, "กรุณาเลือกหน่วยงาน");
                cboRadBgmProjSeq.Visible = false;
                cboRadSubAct.Visible = false;
                cboRadMainAct.Visible = false;
                cboRadOutput.Visible = false;
                cboRadTarget.Visible = false;
                btnRadSubmit.Visible = false;
                lblProj.Visible = false;
                lblTarget.Visible = false;
                hideGrid();
            }
        }

        protected void RadManager1_AjaxReQuest(object sender, AjaxRequestEventArgs e)
        {
            LoadgrdMonth();
            LoadgrdQuarter();
            LoadgrdAnnual();
        }

        protected void cboRadSubAct_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (cboRadSubAct.SelectedIndex > 0)
            {
                btnRadSubmit.Visible = true;
                btnRadSubmit.Text = "แสดงตัวชี้วัดกิจกรรมย่อย";
            }
        }

        protected void cboRadMainAct_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (cboRadMainAct.SelectedIndex > 0)
            {
                btnRadSubmit.Visible = true;
                btnRadSubmit.Text = "แสดงตัวชี้วัดกิจกรรมหลัก";
            }
        }

        protected void cboRadOutput_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (cboRadOutput.SelectedIndex > 0)
            {
                btnRadSubmit.Visible = true;
                btnRadSubmit.Text = "แสดงตัวชี้วัดผลผลิต";
            }
        }

        protected void cboRadBgmProjSeq_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (cboRadBgmProjSeq.SelectedIndex > 0)
            {
                btnRadSubmit.Visible = true;
                btnRadSubmit.Text = "แสดงตัวชี้วัดโครงการ";
            }
        }

        protected void btnRadSubmit_Click(object sender, EventArgs e)
        {

            switch (cboRadTarget.SelectedValue)
            {
                case "1":
                    grdRadMonth.Visible = true;
                    grdRadQuarter.Visible = false;
                    grdRadAnnual.Visible = false;
                    LoadgrdMonth();
                    break;

                case "2":
                    grdRadQuarter.Visible = true;
                    grdRadMonth.Visible = false;
                    grdRadAnnual.Visible = false;
                    LoadgrdQuarter();
                    break;

                case "3":
                    grdRadAnnual.Visible = true;
                    grdRadMonth.Visible = false;
                    grdRadQuarter.Visible = false;
                    LoadgrdAnnual();
                    break;
            }
        }

        protected void hideControl()
        {
            cboRadJobSite.Visible = false;
            cboRadBgmProjSeq.Visible = false;
            cboRadSubAct.Visible = false;
            cboRadMainAct.Visible = false;
            cboRadOutput.Visible = false;
            cboRadTarget.Visible = false;
            btnRadSubmit.Visible = false;
            lblProj.Visible = false;
            lblTarget.Visible = false;
            Label1.Visible = false;
        }

        protected void hideGrid()
        {
            grdRadMonth.Visible = false;
            grdRadQuarter.Visible = false;
            grdRadAnnual.Visible = false;
        }
    }
}